package cn.javadog.algorithm.e1_100;

import java.util.Arrays;

/**
 * 加一
 * https://leetcode-cn.com/problems/plus-one/
 * @author Sprite
 */
public class Algorithm66 {
	public static void main(String[] args) {
		int[] digits = new int[]{9,9,8};
		int[] plusOne = plusOne(digits);
		Arrays.stream(plusOne).forEach(System.out::println);
	}

	public static int[] plusOne(int[] digits) {
		// 最后一位加1
		digits[digits.length-1] = digits[digits.length-1]+1;
		boolean over = false;
		for (int i = digits.length-1; i >= 0; i--) {
			if (over) digits[i]++;

			over = digits[i] == 10;
			if (over) digits[i] = 0;
			else break;
		}
		// 如果没有over，直接返回
		if (!over){
			return digits;
		}else {
			int[] copy = new int[digits.length+1];
			System.arraycopy(digits,0,copy,1,digits.length);
			copy[0] = 1;
			return copy;
		}
	}
}
